package collection;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class ListDemo {
    public static void main(String[] args) {
        List<String> list=new ArrayList<>();//LinkedList<>结果一样
        list.add("one");
        list.add("two");
        list.add("three");
        list.add("four");
        System.out.println(list);

        //获取
        String s=list.get(2);//获取list集合中第三个元素
        System.out.println(s);//three

        Iterator<String> it=list.iterator();
        while(it.hasNext()){
            String str=it.next();
            System.out.println(str);
        }
        for(String s1:list){
            System.out.println(s1);
        }

        //E set(int index,E e):将给定元素设置到指定位置,返回被替换的元素--一般不接收
        //list.set(2,"five");//替换
        //System.out.println(list);//[one, two, five, four]
        String o=list.set(0,"nine");
        System.out.println(o);//one

        //E remove(int index);删除
        list.remove(2);
        System.out.println(list);//[nine, two, four]

        //void add(int index,E e)
        //添加
        list.add(1,"six");
        System.out.println(list);//[nine, six, two, four]













    }
}
